package com.hhit.bigscreenbackmanage.service.impl.tgygl;

import com.hhit.bigscreenbackmanage.dao.tgygl.TTgyglMonthBuildPlanPtscMapper;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglLicenseProcessingPlan;
import com.hhit.bigscreenbackmanage.entity.tgygl.TTgyglMonthBuildPlanPtsc;
import com.hhit.bigscreenbackmanage.service.tgygl.TTgyglMonthBuildPlanPtscService;
import org.seimicrawler.xpath.JXNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author zhl
 * @Date 2021/7/22
 */
@Service
public class TTgyglMonthBuildPlanPtscServiceImpl implements TTgyglMonthBuildPlanPtscService {

    @Autowired
    private TTgyglMonthBuildPlanPtscMapper tTgyglMonthBuildPlanPtscMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insert(TTgyglMonthBuildPlanPtsc record) {
        return 0;
    }

    @Override
    public int insertSelective(TTgyglMonthBuildPlanPtsc record) {
        return 0;
    }

    @Override
    public TTgyglMonthBuildPlanPtsc selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(TTgyglMonthBuildPlanPtsc record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(TTgyglMonthBuildPlanPtsc record) {
        return 0;
    }

    @Override
    public void insertWord(JXNode jxNode, int id, int year, int month) throws Exception {

        //先删除对应的数据
        tTgyglMonthBuildPlanPtscMapper.deleteByNfAndFf(year,month);

        List<JXNode> trNodeList = jxNode.sel("//tr");
        //获取第一行的进度目标;
        List<JXNode> projectName = trNodeList.get(0).sel("//td");
        String projectName1 = projectName.get(1).selOne("//p//text()").toString();
        String projectName2 = projectName.get(2).selOne("//p//text()").toString();
        String projectName3 = projectName.get(3).selOne("//p//text()").toString();

        TTgyglMonthBuildPlanPtsc tTgyglMonthBuildPlanPtsc;
        //铁塔组立累计完成数量
        String number = "";
        //保存数据
        for (JXNode item : trNodeList) {
            int index = trNodeList.indexOf(item);
            if (index == 1) {
                List<JXNode> tdNodeList = item.sel("//td");
                tTgyglMonthBuildPlanPtsc = new TTgyglMonthBuildPlanPtsc();
                //年
                tTgyglMonthBuildPlanPtsc.setYear(year);
                //月
                tTgyglMonthBuildPlanPtsc.setMonth(month);
                //文件id
                tTgyglMonthBuildPlanPtsc.setFileId(id);
                //备注
                tTgyglMonthBuildPlanPtsc.setRemarks(tdNodeList.get(4).selOne("//p//text()").toString());
                //标段名称
                tTgyglMonthBuildPlanPtsc.setSectionName(tdNodeList.get(0).selOne("//p//text()").toString());

                //第一行遇到'合计就跳过'
                JXNode node = tdNodeList.get(0).selOne("//p//text()");
                if (node == null || (node.toString().contains("合") && node.toString().contains("计"))) {
                    continue;
                }


                //第一条数据
                //标题
                tTgyglMonthBuildPlanPtsc.setProjectName(projectName1);
                //内容
                tTgyglMonthBuildPlanPtsc.setContent(tdNodeList.get(1).selOne("//p//text()").toString());
                //保存
                tTgyglMonthBuildPlanPtscMapper.insert(tTgyglMonthBuildPlanPtsc);

                //第二条数据
                //标题
                tTgyglMonthBuildPlanPtsc.setProjectName(projectName2);
                //内容
                tTgyglMonthBuildPlanPtsc.setContent(tdNodeList.get(2).selOne("//p//text()").toString());
                //保存
                tTgyglMonthBuildPlanPtscMapper.insert(tTgyglMonthBuildPlanPtsc);

                //第三条数据
                //标题
                tTgyglMonthBuildPlanPtsc.setProjectName(projectName3);
                //内容
                //给下面的也赋值
                number = tdNodeList.get(3).selOne("//p//text()").toString() == null ? "" : tdNodeList.get(3).selOne("//p//text()").toString();
                tTgyglMonthBuildPlanPtsc.setContent(number);
                //保存
                tTgyglMonthBuildPlanPtscMapper.insert(tTgyglMonthBuildPlanPtsc);

            }

            if (index > 1) {
                List<JXNode> tdNodeList = item.sel("//td");
                tTgyglMonthBuildPlanPtsc = new TTgyglMonthBuildPlanPtsc();
                //年
                tTgyglMonthBuildPlanPtsc.setYear(year);
                //月
                tTgyglMonthBuildPlanPtsc.setMonth(month);
                //文件id
                tTgyglMonthBuildPlanPtsc.setFileId(id);
                //备注
                tTgyglMonthBuildPlanPtsc.setRemarks(tdNodeList.get(3).selOne("//p//text()").toString());
                //标段名称
                tTgyglMonthBuildPlanPtsc.setSectionName(tdNodeList.get(0).selOne("//p//text()").toString());

                //第一行遇到'合计就跳过'
                JXNode node = tdNodeList.get(0).selOne("//p//text()");
                if (node == null || (node.toString().contains("合") && node.toString().contains("计"))) {
                    continue;
                }

                //第一条数据
                //标题
                tTgyglMonthBuildPlanPtsc.setProjectName(projectName1);
                //内容
                tTgyglMonthBuildPlanPtsc.setContent(tdNodeList.get(1).selOne("//p//text()").toString());
                //保存
                tTgyglMonthBuildPlanPtscMapper.insert(tTgyglMonthBuildPlanPtsc);

                //第二条数据
                //标题
                tTgyglMonthBuildPlanPtsc.setProjectName(projectName2);
                //内容
                tTgyglMonthBuildPlanPtsc.setContent(tdNodeList.get(2).selOne("//p//text()").toString());
                //保存
                tTgyglMonthBuildPlanPtscMapper.insert(tTgyglMonthBuildPlanPtsc);

                //第三条数据
                //标题
                tTgyglMonthBuildPlanPtsc.setProjectName(projectName3);
                //内容
                //给下面的也赋值
                tTgyglMonthBuildPlanPtsc.setContent(number);
                //保存
                tTgyglMonthBuildPlanPtscMapper.insert(tTgyglMonthBuildPlanPtsc);

            }
            System.out.println(item);
        }
    }

    @Override
    public List<TTgyglMonthBuildPlanPtsc> selectListByFileId(Integer fileId) {
        List<TTgyglMonthBuildPlanPtsc> resulList = null;
        TTgyglMonthBuildPlanPtsc tTgyglMonthBuildPlanPtsc = new TTgyglMonthBuildPlanPtsc();
        tTgyglMonthBuildPlanPtsc.setFileId(fileId);
        try {
            resulList = tTgyglMonthBuildPlanPtscMapper.selectByBean(tTgyglMonthBuildPlanPtsc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resulList;
    }
}
